// antd 按需加载
import { Toast, Portal } from '@ant-design/react-native';

import store from '../../../store/store.js'
import * as ActionTypes from '../../../constants/constants.js'
import { isUser, isWxUser, path, saveData, loadData, removeData, requstPromise, requestMessage } from '../../../util/utils.js'
import request from '../../../api/request.js'

export async function getShopCartList(options, loading) {
    const { dispatch } = this.props
    // const { refresh } = options
    const res = await request.CartController.list(options)
    if (loading) {
        Portal.remove(loading)
    }
    if (res.error) {
        dispatch({
            type: ActionTypes.shoppingCartListError,
            data: res
        })
        return
    }
    // dispatch({
    //     type: ActionTypes.shoppingCartListRefresh,
    //     data: res.data.data
    // })
    dispatch({
        type: ActionTypes.shoppingCartList,
        data: res.data.data
    })

    // res.data.data.map(item => {
    //     item.checked = false
    // })
    // this.setState({
    //     checkedList: res.data.data
    // })

    return requstPromise(res)
}

export async function generateConfirmOrder(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('提交订单', 0, null, false)
    const res = await request.PortalOrderController.generateConfirmOrder(options)

    if (loading) {
        Portal.remove(loading)
    }

    dispatch({
        type: ActionTypes.confirmOrder,
        data: res.data.data
    })

    return requstPromise(res)
}

export async function updateQuantity(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.CartController.updateQuantity(options)
    Portal.remove(loading)
    return requstPromise(res)
}

export async function updateChecked(options) {
    const { dispatch } = this.props
    console.log('修改中')
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.CartController.updateChecked(options)
    Portal.remove(loading)
    console.log(res)
    return requstPromise(res)
}

export async function deleteCart(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('删除中', 0, null, false)
    const res = await request.CartController.delete(options)
    return requestMessage(res, loading)
}


